/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.scene.entity.SceneSecurityGroupRule;
import com.lczyfz.istep.modules.scene.service.SceneSecurityGroupRuleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 安全组规则Controller
 * @author dh
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneSecurityGroupRule")
public class SceneSecurityGroupRuleController extends BaseController {

	@Autowired
	private SceneSecurityGroupRuleService sceneSecurityGroupRuleService;
	
	@ModelAttribute
	public SceneSecurityGroupRule get(@RequestParam(required=false) String id) {
		SceneSecurityGroupRule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneSecurityGroupRuleService.get(id);
		}
		if (entity == null){
			entity = new SceneSecurityGroupRule();
		}
		return entity;
	}
	
	@RequiresPermissions("scene:sceneSecurityGroupRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneSecurityGroupRule sceneSecurityGroupRule, HttpServletRequest request, HttpServletResponse response, Model model,String securityGroupId) {
		sceneSecurityGroupRule.setSecurityGroupId(securityGroupId);
		logger.debug("dingh====securityGroupId:[{}]",securityGroupId);
		Page<SceneSecurityGroupRule> page = sceneSecurityGroupRuleService.findPage(new Page<SceneSecurityGroupRule>(request, response), sceneSecurityGroupRule);
		model.addAttribute("page", page);
		return "modules/scene/sceneSecurityGroupRuleList";
	}

	@RequiresPermissions("scene:sceneSecurityGroupRule:view")
	@RequestMapping(value = "form")
	public String form(SceneSecurityGroupRule sceneSecurityGroupRule, Model model) {
		model.addAttribute("sceneSecurityGroupRule", sceneSecurityGroupRule);
		return "modules/scene/sceneSecurityGroupRuleForm";
	}

	@RequiresPermissions("scene:sceneSecurityGroupRule:edit")
	@RequestMapping(value = "save")
	public String save(SceneSecurityGroupRule sceneSecurityGroupRule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sceneSecurityGroupRule)){
			return form(sceneSecurityGroupRule, model);
		}
		sceneSecurityGroupRuleService.save(sceneSecurityGroupRule);
		addMessage(redirectAttributes, "保存安全组成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneSecurityGroupRule/?repage";
	}
	
	@RequiresPermissions("scene:sceneSecurityGroupRule:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneSecurityGroupRule sceneSecurityGroupRule, RedirectAttributes redirectAttributes) {
		sceneSecurityGroupRuleService.delete(sceneSecurityGroupRule);
		addMessage(redirectAttributes, "删除安全组成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneSecurityGroupRule/?repage";
	}

}